 var appPath = getAppPath();
 // 定义 根据下标删除数组元素
 Array.prototype.remove=function(obj){ 
	for(var i =0;i <this.length;i++){ 
		var temp = this[i]; 
		if(!isNaN(obj)){ 
			temp=i; 
		} 
		if(temp == obj){ 
			for(var j = i;j <this.length;j++){ 
				this[j]=this[j+1]; 
			} 
			this.length = this.length-1; 
		} 
	} 
} 

$(function(){
	initJspGrid();
});


function cancelDown(){
	$('#downDlg').window('close');
}
// 样版列表初始化
function initJspGrid(){
	$('#qaInfoGrid').datagrid({
	    url:appPath + "/yjxRedis/getQaInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    columns:[[	     
	        {field:'info_id',title:'记录ID',width:20,align:'center',sortable:false},
	        {field:'qa_id',title:'问卷模板ID',width:20,align:'center',sortable:false},
	        {field:'user_id',title:'用户ID',width:20,align:'center',sortable:false},
	        {field:'data',title:'答案信息',width:30,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if (value){
	        			return '<a href="javascript:void(0)" onclick="showData('+JSON.stringify(value).replace(/"/g, '&quot;')+','+rowData.qa_id+')">查看</a>';
	        		}
	        		return '';
	        	}
	        },
	        {field:'mobile',title:'手机号',width:30,align:'center',sortable:false},
	        {field:'mail',title:'邮箱',width:30,align:'center',sortable:false},
	        {field:'qq',title:'QQ号',width:30,align:'center',sortable:false},
	        {field:'addr',title:'地址',width:30,align:'center',sortable:false},
	        {field:'desc',title:'订单详情',width:30,align:'center',sortable:false},
	        {field:'update_time',title:'修改时间',width:40,align:'center',sortable:false},
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	});
}
/**	查看data
 * @param id qaData 问题答案
 */
function showData(qaData,id){
	$.loading(true);
	$("#dataForm").form('clear');
	$('#qaTmplData').window({
		title:'问卷信息',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#qaTmplData').window('move',{top:20,left:200});
	$('#qaTmplData').window('open');
	var url =appPath+"/yjxRedis/getQaTmplInfo.action?qaId="+id;
	$.get(url,function(ret){
		var json = ret[0];
		var data = json.data;
		var arry = JSON.parse(data).questions;
		var htm = '';
		var spanId = '';
		for(var k=0;k<arry.length;k++){
			var quest = arry[k];
			var qTitle = quest.title;// 问题标题
			var qId = quest.id;// 问题序号
			var optionsArry = quest.options;
			htm += '<tr><td style="color:red">ID'+qId+':&nbsp&nbsp'+qTitle+'</td><tr/>';
			
			// 题目选项列表
			// 6个选项   第三个 和第五个
			// 
			var answerArry = JSON.parse(qaData)[k+1];// 答案
			
			for(var i=0;i<optionsArry.length;i++){
				var opId = optionsArry[i].id;// 题目选项ID
				var opType = optionsArry[i].type;// 题目选项类型
				
				
				// 答案列表
				var isanwser = false;
				for(var m=0;m<answerArry.length;m++){
					var aId = Number(answerArry[m].id);// 答案ID
					var aType = Number(answerArry[m].type);// 答案类型
					if (aId==opId && aType==opType && aType==10){// 判断如果等于10 <span align="right" style="float:right;color:#8B8970;">已勾选</span></td>
						htm +='<tr><td bgcolor="#FBEC88">'+optionsArry[i].title+'</tr>';
//						answerArry = answerArry.splice(m,1);// 删掉下标为m的元素
						answerArry.remove(m);
						isanwser =true;
					} else if(aId==opId && aType==opType && aType==11) {
						htm +='<tr><td bgcolor="#FBEC88">'+optionsArry[i].title+'<input type="text" value="'+answerArry[m].content+'" style="float:right;"/></td></tr>';
						answerArry.remove(m);
						isanwser =true;
					} else if(aId==opId && aType==opType && aType==20) {
						htm +='<tr><td bgcolor="#FBEC88">填写内容：<input type="text" value="'+answerArry[m].content+'" style="float:right;"/></td></tr>';
						answerArry.remove(m);
						isanwser =true;
					}
				}
				
				if (!isanwser){
						htm +='<tr><td>'+optionsArry[i].title+'</td></tr>';
				}
			}
		}
		// html的内容
		$("#tmplDataTable").html(htm);
		$.loading(false);
	});
}

//判断日期格式[YYYY-MM-DD]
function chkInputDate(dt){
	 var strDate=dt+'';
    var ss = (strDate.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        var chkDt = new Date(y,m-1,d);
		if((chkDt.getFullYear()==y) &&((chkDt.getMonth()+1)==m) &&(chkDt.getDate()==d)) {
        	return dtFmt(chkDt);
        }
    }
    return '';
}

function getQueryParam(){
	var queryObject = {};
	var findMobile= $("#queryMobile").val();
	var findQaTmplId = $("#queryQaTmplId").val();
	if (findMobile && Number(findMobile)>0) queryObject.findMobile = findMobile;// 通过手机号 查询调查问卷
	if (findQaTmplId && Number(findQaTmplId)>0) queryObject.findQaTmplId = findQaTmplId;// 问卷调查ID
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#qaInfoGrid").datagrid('unselectAll');
	$("#qaInfoGrid").datagrid('load', getQueryParam());
}
